package com.itheima.fanout_optimize;

import com.itheima.utils.RabbitMqUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author Administrator
 */
public class Producer {

    /**
     * 定义交换机
     */
    private static final String FANOUT_EXCHANGE_OPTIMIZE= "fanout_exchange_optimize";


    public static void main(String[] args) throws Exception {
        //获取连接
        Connection connection = RabbitMqUtil.newConnection();

        //3.创建频道
        Channel channel = connection.createChannel();

        //创建交换机
        /**
         * exchangeDeclare(String exchange, String type)
         * 参数1 exchange : 交换机名称
         * 参数2 type : 交换机类型
         */
        channel.exchangeDeclare(FANOUT_EXCHANGE_OPTIMIZE, BuiltinExchangeType.FANOUT);

        for (int i = 0; i < 30; i++) {

            //5.要发送的消息
            String message = "hello world work !" + i;

            //发送消息
            /**
             * basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
             * 参数1 exchange :交换机名称 如果没有则使用默认的Exchange
             * 参数2 routingKey : 路由Key 简单模式和工作模式下使用队列名称来代替路由Key
             * 参数3 props : 消息的其他属性
             * 参数4 body : 要发送的消息
             */
            channel.basicPublish(FANOUT_EXCHANGE_OPTIMIZE, "", null, message.getBytes());
        }

        //6.关闭资源
        channel.close();
        connection.close();


    }

}
